package com.zl.learning.db.postgresql;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by zhaolei on 2017/6/16.
 */
public class TestForEach {
    @Test
    public void test1() {
        String[] books = {"java权威指南", "js权威指南", "jquery权威指南"};
        for (String book : books) {
            book = "aaa";
        }
        System.out.println(books[0]);
        for (int i = 0; i < books.length; i++) {
            books[i] = "bbb";
        }
        System.out.println(books[0]);
        List<String> l = Arrays.asList(books).stream().map(b -> b + b).collect(Collectors.toList());
        l.forEach(r -> System.out.println(l));
        String str = new String("aaa");
    }

    @Test
    public void test2() throws IOException {
        Map<String, String> env = System.getenv();
        env.forEach((k, v) -> System.out.println(k + "=====" + v));
        System.out.println("=======================================");
        Properties props = System.getProperties();
        props.forEach((k, v) -> System.out.println(k + "=====" + v));
    }

    @Test
    public void test3() {
        String[] args = {"zh", "CN"};
        Locale currentLocale = null;
        if (args.length == 2) {
            currentLocale = new Locale(args[0], args[1]);
        } else {
            currentLocale = Locale.getDefault();
        }
        ResourceBundle bundle = ResourceBundle.getBundle("MyResource", currentLocale);
        String msg = bundle.getString("msg");
//        String msg = "{0}++++{1}";
        System.out.println(MessageFormat.format(msg, "yeeku", new Date()));
    }

    @Test
    public void test4(){
        CharBuffer buff = CharBuffer.allocate(8);
        System.out.println("capacity:"+buff.capacity());
        System.out.println("limit:"+buff.limit());
        System.out.println("position:"+buff.position());
        buff.put('a');
        buff.put('b');
        buff.put('c');
        System.out.println("===================");
        System.out.println("capacity:"+buff.capacity());
        System.out.println("limit:"+buff.limit());
        System.out.println("position:"+buff.position());
        buff.flip();
        System.out.println("===================");
        System.out.println("capacity:"+buff.capacity());
        System.out.println("limit:"+buff.limit());
        System.out.println("position:"+buff.get());
        buff.clear();
        System.out.println("===================");
        System.out.println("capacity:"+buff.capacity());
        System.out.println("limit:"+buff.limit());
        System.out.println("position:"+buff.get());
    }

    @Test
    public void test5() throws IOException {
        FileChannel inChannel = null;
        FileChannel outChannel = null;

        File f = new File("D:\\test.txt");
        inChannel = new FileInputStream(f).getChannel();
        MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY,0,f.length());
        Charset charset = Charset.forName("gbk");
        outChannel = new FileOutputStream("a.txt").getChannel();
        outChannel.write(buffer);
        buffer.clear();
        CharsetDecoder decoder = charset.newDecoder();
        CharBuffer charBuffer = decoder.decode(buffer);
        System.out.println(charBuffer);
        inChannel.close();
        outChannel.close();
    }

    public static void main(String[] args) throws IOException {
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        String buffer = null;
//        while((buffer=br.readLine())!=null){
//            System.out.println("用户键盘输入的是:"+buffer);
//        }
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            System.out.println("用户键盘输入的是:" + in.nextLine());
        }
    }
}
